package com.example.jones.test;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import com.example.jones.function.FunctionView;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.RelativeLayout;

public class MainActivity extends Activity {

    private com.example.jones.test.TagsGridView gv_menu = null;
    private MenuAdepter mdt = null;
    private HomeView homeview = null;
    private MatchView mathcview;
    private SearchCBView searchview;
    private FunctionView functionview;
    private RelativeLayout rl_content = null;
    private String[] _title = { "每日推荐", "原料配菜", "搜索", "功能" };

    List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (Float.valueOf(android.os.Build.VERSION.RELEASE.substring(0, 3)
                .trim()) > 2.3)
        // 想要在主线程中访问网络必须能添加以下代码块，否则就要用异步的方式处理
        {
            StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
                    .detectDiskReads().detectDiskWrites().detectNetwork()
                    .penaltyLog().build());

            StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
                    .detectLeakedSqlLiteObjects().penaltyLog().penaltyDeath()
                    .build());
        }

        rl_content = (RelativeLayout) findViewById(R.id.rl_content);
        gv_menu = (TagsGridView) findViewById(R.id.gv_menu);// 底部菜单————"每日推荐",
                                                            // "原料配菜", "搜索",
                                                            // "功能"

        homeview = new HomeView(this);
        rl_content.addView(homeview.getView());

        mdt = new MenuAdepter(_title);
        gv_menu.setAdapter(mdt);

        gv_menu.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                mdt.setPotion(arg2);
                mdt.notifyDataSetChanged();
                switch (arg2) {
                case 0:// 每日推荐
                    rl_content.removeAllViews();
                    if (homeview == null) {
                        homeview = new HomeView(MainActivity.this);
                    }
                    rl_content.addView(homeview.getView());
                    break;
                case 1:// 原料配菜
                    rl_content.removeAllViews();
                    if (mathcview == null) {
                        mathcview = new MatchView(MainActivity.this);
                    }
                    try {
                        rl_content.addView(mathcview.getView());
                    } catch (JSONException e) {
                        e.printStackTrace();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
                case 2:// 搜索菜谱
                    rl_content.removeAllViews();
                    if (searchview == null) {
                        searchview = new SearchCBView(MainActivity.this);
                    }
                    try {
                        rl_content.addView(searchview.getView());
                    } catch (JSONException e) {
                        e.printStackTrace();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
                case 3:// 功能
                    rl_content.removeAllViews();
                    if (functionview == null) {
                        functionview = new FunctionView(MainActivity.this);
                    }
                    try {
                        rl_content.addView(functionview.getView());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
                }
            }
        });
    }
    
//    @Override
//    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//        mTencent.onActivityResult(requestCode, resultCode, data);
//    }
}
